#include "PrecompiledHeader.h"
#include "ICriticalSection.h"

/*--------------------------------------------------------------------------*/

ICriticalSection::ICriticalSection(uint i)
{
	this->m_mutexes = new SDL_mutex*[i];
	this->m_sectionsCount = i;

	for (uint r = 0; r < i; ++r)
	{
		this->m_mutexes[r] = SDL_CreateMutex();
	}
}

/*--------------------------------------------------------------------------*/

ICriticalSection::~ICriticalSection()
{
	for (uint r = 0; r < this->m_sectionsCount; ++r)
	{
		SDL_DestroyMutex(this->m_mutexes[r]);
	}

	delete [] this-> m_mutexes;
}

/*--------------------------------------------------------------------------*/

void ICriticalSection::Lock(uint m) const
{
	if (m < this->m_sectionsCount)
	{
		SDL_LockMutex(this->m_mutexes[m]);
	}
}

/*--------------------------------------------------------------------------*/

void ICriticalSection::Unlock(uint m) const
{
	if (m < this->m_sectionsCount)
	{
		SDL_UnlockMutex(this->m_mutexes[m]);
	}
}

/*--------------------------------------------------------------------------*/